#!/bin/bash

REMOTE_SERVER_NAME=""
FILE_PATH=""
CONFIG_FILE=""
while getopts ":f:r:c:" opt; do
  case $opt in
  r)
    REMOTE_SERVER_NAME=$OPTARG
    ;;
  f)
    FILE_PATH=$OPTARG
    ;;
  c)
    CONFIG_FILE=$OPTARG
    ;;
  ?)
    echo "可用选项：-r 远程服务器名称 -f 上传的文件路径 -c 配置文件路径，默认脚本同目录下config/remote-servers.json"
    exit 1
    ;;
  esac
done

if [ -z "${REMOTE_SERVER_NAME}" ]; then
  echo "未指定远程服务器名称 -r"
  exit
fi

if [ -z "${FILE_PATH}" ]; then
  echo "未指定上传的文件路径 -f"
  exit
fi

# 获取配置文件信息，可在命令行指定
remote_servers_info_json=""
if [ -n "${CONFIG_FILE}" ]; then
  if [ -f "${CONFIG_FILE}" ]; then
    file_type=$(file -b "${CONFIG_FILE}")
    if [[ ! (${file_type} == "JSON data") ]]; then
      echo "${CONFIG_FILE}不是一个JSON类型文件或文件内容为空"
      exit 1
    fi
    remote_servers_info_json="$(cat "${CONFIG_FILE}")"
  else
    echo "配置文件：${CONFIG_FILE}不是一个文件或不存在"
    exit 1
  fi
else
  echo "未指定配置文件路径，默认使用脚本同目录下config/remote-servers.json"
  remote_servers_info_json="$(cat config/remote-servers.json)"
fi

if [ ${#remote_servers_info_json} -eq 0 ]; then
  echo "配置文件config/remote-server.json内容为空"
  exit 1
fi

match_remote_server_json_array=()
remote_server_info_length=$(echo "${remote_servers_info_json}" | jq ".server_list | length")
for ((i = 0; i < "${remote_server_info_length}"; i++)); do
  json_object=$(echo "${remote_servers_info_json}" | jq -r ".server_list[${i}]")
  server_name=$(echo "${json_object}" | jq -r ".name")
  if [[ ${server_name} =~ ${REMOTE_SERVER_NAME} ]]; then
    match_remote_server_json_array[${#match_remote_server_json_array[@]}]=${json_object}
    break
  fi
done

if [ ${#match_remote_server_json_array[@]} -eq 0 ]; then
  echo "部署的服务名称${REMOTE_SERVER_NAME}在配置文件中[config/remote-server.json]找不到匹配项"
  exit
fi

match_remote_server_json=${match_remote_server_json_array[0]}
server_name=$(echo "${match_remote_server_json}" | jq -r ".name")

echo "输入的服务器名称：${REMOTE_SERVER_NAME} 匹配到的完整名称：${server_name}"
#echo "完整的服务器配置："${match_remote_server_json}""

# 判断上传文件是否存在
if [[ ! -f ${FILE_PATH} ]]; then
  echo "${FILE_PATH}不是一个文件或不存在"
  exit
fi

FILE_PATH=$(readlink -f "${FILE_PATH}")

echo "上传的文件信息:"
file_size=$(stat "${FILE_PATH}" -c '%s')
echo "  路径: ${FILE_PATH}"
echo "  类型: $(file -b "${FILE_PATH}")"
echo "  大小: $(echo | awk "{print int(${file_size}/1024/1024)}")MB"

server_host=$(echo "${match_remote_server_json}" | jq -r ".host")
server_username=$(echo "${match_remote_server_json}" | jq -r ".username")
server_dest_dir=$(echo "${match_remote_server_json}" | jq -r ".dest_dir")
server_auth_type=$(echo "${match_remote_server_json}" | jq -r ".auth_type")
server_exe_script=$(echo "${match_remote_server_json}" | jq -r ".script")

echo "将本地文件${FILE_PATH}上传到远程服务器：${server_username}@${server_host}:${server_dest_dir}"
read -r -p "是否执行上传操作? [Y/n] " input
case $input in
[yY][eE][sS] | [yY])
  echo "开始执行"
  ;;
[nN][oO] | [nN])
  echo "取消执行"
  exit 0
  ;;
*)
  echo "Invalid input..."
  exit 1
  ;;
esac

file_name=$(basename "${FILE_PATH}" .jar)
server_exe_script=$(echo "${server_exe_script}" | sed "s/_service_name_/${file_name}/")

echo "${server_exe_script}"

# 免密
if [ "${server_auth_type}" = "noSecret" ]; then
  # 备份旧文件
  eval "ssh ${server_username}@${server_host} 'bash -s' < backup-remote.sh $(basename "${FILE_PATH}") ${server_dest_dir}"
  # 上传文件
  eval "scp ${FILE_PATH} ${server_username}@${server_host}:${server_dest_dir}"
  # 执行脚本
  ssh ${server_username}@${server_host} "${server_exe_script}"
fi

# 密码
if [ "${server_auth_type}" = "password" ]; then
  server_password=$(echo "${match_remote_server_json}" | jq -r ".password")
  # 备份旧文件
  eval "sshpass -p ${server_password} ssh ${server_username}@${server_host} 'bash -s' < backup-remote.sh $(basename "${FILE_PATH}") ${server_dest_dir}"
  # 上传文件
  eval "sshpass -p ${server_password} scp ${FILE_PATH} ${server_username}@${server_host}:${server_dest_dir}"
  # 执行脚本
  sshpass -p ${server_password} ssh ${server_username}@${server_host} "${server_exe_script}"
fi

# 密钥文件
if [ "${server_auth_type}" = "identityFile" ]; then
  server_identity_file=$(echo "${match_remote_server_json}" | jq -r ".identity_file")
  # 备份旧文件
  eval "ssh -i ${server_identity_file} ${server_username}@${server_host} 'bash -s' < backup-remote.sh $(basename "${FILE_PATH}") ${server_dest_dir}"
  # 上传文件
  eval "scp -i ${server_identity_file} ${FILE_PATH} ${server_username}@${server_host}:${server_dest_dir}"
  # 执行脚本
  ssh -i ${server_identity_file} ssh ${server_username}@${server_host} "${server_exe_script}"
fi
